package com.leTest.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leTest.eduService.entity.TeacherEntity;
import com.leTest.eduService.entity.vo.TeacherQuery;
import com.leTest.eduService.mapper.TeacherMapper;
import com.leTest.eduService.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author le
 * @since 2025-02-06
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, TeacherEntity> implements TeacherService {


    @Override
    public Map getTeacherListByCondition(Long current, Long limit, TeacherQuery teacherQuery) {
        Page<TeacherEntity> pageT = new Page<>(current, limit);
        // 构建条件
        QueryWrapper<TeacherEntity> queryWrapper = new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空,如果不为空就拼接该条件值
        if (!StringUtils.isEmpty(name)) {
            //构建条件
            //模糊查询
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            //构建条件
            queryWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            //构建条件
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            //构建条件
            queryWrapper.le("gmt_create", end);
        }
        //使用创建时间做降序排序
        queryWrapper.orderByDesc("gmt_create");

        this.page(pageT, queryWrapper);
        Map resultMap = new HashMap();
        List<TeacherEntity> list = pageT.getRecords();
        resultMap.put("total", pageT.getTotal());
        resultMap.put("rows", pageT.getRecords());

        return resultMap;
    }
}
